<!--
  module - 设备管理

  @author lgx 2021-06-07
-->
<template>
  <div class="mixin-components-container">
    <el-col :span="24">
      <el-card>
        <div slot="header" class="clearfix">
          <template>
            <el-page-header :content="titleContent" @back="goBack" />
          </template>
        </div>
        <div>
          <div>
            <div style="margin-bottom:50px;">
              <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="140px"
                style="padding: 25px 0;"
                :disabled="$route.query.type === '3' ? true : false"
              >
                <el-row :gutter="20">
                  <el-col :span="10">
                    <el-form-item prop="name" label="设备名称">
                      <el-input v-model="form.name" placeholder="请填写设备名称" maxlength="50" clearable />
                    </el-form-item>
                    <el-form-item prop="passWord" label="设备密码">
                      <el-input v-model="form.passWord" type="string" placeholder="请填写密码" />
                    </el-form-item>
                    <el-form-item prop="contacts" label="负责人">
                      <el-input v-model="form.contacts" type="string" placeholder="请填写负责人" />
                    </el-form-item>
                    <el-form-item prop="enterpriseId" label="设备所属企业id">
                      <!-- <el-cascader v-model="form.enterpriseId" :options="options" placeholder="请填写设备所属企业id" maxlength="50" :props="treeProps" show-word-limit clearable /> -->
                      <el-select v-model="form.enterpriseId" filterable placeholder="请选择设备所属企业id">
                        <el-option
                          v-for="item in options"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        />
                      </el-select>
                    </el-form-item>
                    <el-form-item prop="replay" label="设备回放地址">
                      <el-input
                        v-model="form.replay"
                        placeholder="请填写设备回放地址"
                        show-word-limit
                        clearable
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="10">
                    <el-form-item prop="code" label="设备编号">
                      <el-input v-model="form.code" type="code" placeholder="请填写设备编号" />
                    </el-form-item>
                    <el-form-item prop="contactsPhone" label="负责人电话">
                      <el-input
                        v-model="form.contactsPhone"
                        placeholder="请填写负责人电话"
                        maxlength="20"
                        show-word-limit
                        clearable
                      />
                    </el-form-item>
                    <el-form-item prop="position" label="设备位置">
                      <el-input
                        v-model="form.position"
                        placeholder="请填写设备位置"
                        show-word-limit
                        clearable
                      />
                    </el-form-item>
                    <el-form-item prop="live" label="设备直播地址">
                      <el-input
                        v-model="form.live"
                        placeholder="请填写设备直播地址"
                        show-word-limit
                        clearable
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <el-row style="padding-left:140px;">
                <!-- <el-button
                  v-if="$route.query.type === '1' || $route.query.type === '2'"
                  waves
                  type="primary"
                  @click="submitForm('form')"
                >
                  保存
                </el-button> -->
                <el-link v-if="$route.query.type === '1' || $route.query.type === '2'" type="primary" size="mini" @click="submitForm('form')">
                  保存
                </el-link>
                <!-- <el-button waves type="info" @click="goBack">取消</el-button> -->
                <el-link type="info" size="mini" @click="goBack">
                  取消
                </el-link>
              </el-row>
            </div>
          </div>
        </div>

      </el-card>
    </el-col>
  </div>
</template>

<script>
// api
import { saveAndUpdate, queryData } from '@/api/device/sysDevice'
import { queryEnterpriseList } from '@/api/enterprise/sysEnterprise'

// 水波纹插件
import waves from '@/directive/waves'

export default {
  name: 'SaveAndEditSysDevice',
  directives: { waves },
  data() {
    return {
      listLoading: false,
      titleContent: '',
      treeProps: {
        checkStrictly: true,
        expandTrigger: 'click',
        label: 'name',
        value: 'departmentId'
      },
      options: [],
      form: {
        deviceId: '',
        name: '',
        code: '',
        enterpriseId: '',
        contacts: '',
        contactsPhone: '',
        password: '',
        live: '',
        replay: '',
        position: '',
        status: false
      },
      rules: {
        name: [
          { required: true, message: '请填写设备名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请填写设备编号', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const curVue = this
    // 设置部门选项
    if (curVue.$route.query.type === '1') {
      curVue.titleContent = '新建设备'
    } else {
      curVue.form.deviceId = curVue.$route.query.data
      if (curVue.$route.query.type === '2') {
        curVue.titleContent = '编辑设备'
      } else if (curVue.$route.query.type === '3') {
        curVue.titleContent = '查看设备'
      }
    }
    this.queryEnterpriseList()
    if (!curVue.form.deviceId) {
      return
    }
    queryData({ deviceId: curVue.form.deviceId }).then(res => {
      if (res.code === 1) {
        console.log(res)
        this.form = res.data
      } else {
        this.noticeUtil.errorMsg('操作失败!')
      }
    })
  },
  methods: {
    queryEnterpriseList() {
      const curVue = this
      const query = {
        page: 0,
        limit: 0
      }
      queryEnterpriseList(query).then(res => {
        if (res.code === 1) {
          console.log(res.data.enterpriseList)
          for (const enterprise of res.data.enterpriseList) {
            curVue.options.push({ value: enterprise.enterpriseId, label: enterprise.name })
          }
        } else {
          curVue.noticeUtil.errorMsg('用户获取失败！')
        }
      })
    },
    goBack() {
      this.$router.go(-1)
    },
    handleAvatarSuccess(res, file) {
      this.head = URL.createObjectURL(file.raw)
    },
    submitForm(formName) {
      const formObj = {
        deviceId: this.form.deviceId,
        name: this.form.name,
        code: this.form.code,
        enterpriseId: this.form.enterpriseId,
        contacts: this.form.contacts,
        contactsPhone: this.form.contactsPhone,
        position: this.form.position
      }
      this.$refs[formName].validate((valid) => {
        console.log(valid)
        saveAndUpdate(formObj).then(res => {
          if (res.code === 1) {
            this.noticeUtil.successMsg('操作成功!')
            this.$router.go(-1)
          } else {
            this.noticeUtil.errorMsg('操作失败!')
          }
        })
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
  .avatar {
    width: 148px;
    height: 148px;
    display: block;
  }
</style>
